import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/building_list_model.dart';
import 'package:ls_work/net/response/floor_list_bean.dart';
import 'package:ls_work/ui/food/send_food_table_page.dart';

import '../net/response/building_list_bean.dart';
import '../res/colors.dart';

class SelectBuildingFloorViewModel extends BaseViewModel {
  final bool needBack;

  var buildingModel = BuildingListModel();

  var buildingId = "";
  var buildingName = "";
  var buildingList = <BuildingListBean>[];
  var floorId = "";
  var floorName = "";
  var floorList = <FloorListBean>[];
  Function(String buildingId, String floorId, String buildingName,
      String floorName)? callback;

  SelectBuildingFloorViewModel(BuildContext context,
      {this.needBack = false, this.callback})
      : super(context);

  Widget contentView() {
    return Row(
      children: [
        ListView.separated(
                itemBuilder: (context, index) {
                  var item = buildingList[index];
                  return CommonText(item.buildingNo)
                      .addToContainer(
                          alignment: Alignment.center,
                          height: 40,
                          color: buildingId == item.id.toString()
                              ? Colors.white
                              : Colours.bg_color)
                      .onTap(() {
                    if (buildingId == item.id.toString()) {
                      return;
                    }
                    buildingId = item.id.toString();
                    buildingName = item.buildingNo??"";
                    notifyListeners();
                    getFloorList(buildingId);
                  });
                },
                separatorBuilder: (context, index) => Divider(
                      height: 1,
                    ),
                itemCount: buildingList.length)
            .setWeight(1),
        ListView.separated(
                itemBuilder: (context, index) {
                  var e = floorList[index];
                  return CommonText(e.floorNo)
                      .addToContainer(
                          width: double.infinity,
                          padding: EdgeInsets.symmetric(
                              horizontal: 10, vertical: 10),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: 8.borderRadius,
                              border: Border.all()))
                      .onTap(() {
                    floorName = e.floorNo??"";
                    floorId = e.id.toString();
                    if (callback == null) {
                      Go().push(SendFoodTablePage(
                        titleName: "${buildingName}-${floorName}",
                        buildingId: buildingId,
                        floorId: floorId,
                      ));
                    } else {
                      if(callback!=null){
                        callback!(buildingId, floorId, buildingName, floorName);
                      }
                    }
                  });
                },
                padding: EdgeInsets.symmetric(vertical: 16, horizontal: 16),
                separatorBuilder: (context, index) => Container(
                      height: 10,
                    ),
                itemCount: floorList.length)
            .setWeight(2)
      ],
    );
  }

  @override
  void onCreated() {
    super.onCreated();
    getBuildingList();
  }

  Future<void> getBuildingList() async {
    showLoadingState();
    try {
      var list = await buildingModel.getBuildingList();
      buildingList = list;
      showContent();
      if (list.isNotEmpty) {
        buildingId = list[0].id.toString();
        buildingName = list[0].buildingNo??"";
        getFloorList(buildingId);
      }
      notifyListeners();
    } catch (e) {
      showErrorState(NetErrorUtils.getNetError(e));
    }
  }

  Future<void> getFloorList(String buildingId) async {
    showLoadingDialog();
    try {
      floorList = await buildingModel.getFloorList(buildingId);
      hideDialog();
      notifyListeners();
    } catch (e) {
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }
}
